package com.ylb.rent.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xiaoleilu.hutool.util.CollectionUtil;
import com.ylb.rent.dao.ApartmentCountryCityMapper;
import com.ylb.rent.entity.ApartmentCountryCity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 import com.ylb.rent.dao.CityMapper;
import  com.ylb.rent.entity.City;
import com.ylb.rent.service.ICityService;

/**
 * 城市Service业务层处理
 * 
 * @author ylb
 * @date 2023-06-29
 */
@Service
public class CityServiceImpl implements ICityService 
{
    @Autowired
    private CityMapper cityMapper;

    @Autowired
    ApartmentCountryCityMapper apartmentCountryCityMapper;

    @Override
    public List<City> selectListByApartmentId(long aId) {
        QueryWrapper<ApartmentCountryCity> apartmentCountryCityQueryWrapper = new QueryWrapper<>();
        apartmentCountryCityQueryWrapper.eq("apartment_id",aId);
        List<ApartmentCountryCity> apartmentCountryCities = apartmentCountryCityMapper.selectList(apartmentCountryCityQueryWrapper);
        List<Long> cityIdList = apartmentCountryCities.stream().map(ApartmentCountryCity::getCityId).collect(Collectors.toList());
        if(CollectionUtil.isNotEmpty(cityIdList)){
            return cityMapper.selectBatchIds(cityIdList);

        }
        return new ArrayList<>();
    }

    /**
     * 查询城市
     * 
     * @param id 城市主键
     * @return 城市
     */
    @Override
    public City selectCityById(Long id)
    {
        return cityMapper.selectCityById(id);
    }

    /**
     * 查询城市列表
     * 
     * @param city 城市
     * @return 城市
     */
    @Override
    public List<City> selectCityList(City city)
    {
        return cityMapper.selectCityList(city);
    }

    /**
     * 新增城市
     * 
     * @param city 城市
     * @return 结果
     */
    @Override
    public int insertCity(City city)
    {
        return cityMapper.insertCity(city);
    }

    /**
     * 修改城市
     * 
     * @param city 城市
     * @return 结果
     */
    @Override
    public int updateCity(City city)
    {
        return cityMapper.updateCity(city);
    }

    /**
     * 批量删除城市
     * 
     * @param ids 需要删除的城市主键
     * @return 结果
     */
    @Override
    public int deleteCityByIds(Long[] ids)
    {
        return cityMapper.deleteCityByIds(ids);
    }

    /**
     * 删除城市信息
     * 
     * @param id 城市主键
     * @return 结果
     */
    @Override
    public int deleteCityById(Long id)
    {
        return cityMapper.deleteCityById(id);
    }
}
